//
//  NewScriptVC.m
//  sweetbox
//
//  Created by apollo on 15/9/18.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "NewScriptVC.h"
#import "DBHelper.h"
#import "ScriptsVC.h"
#import "log.h"

@interface NewScriptVC ()

@property (weak, nonatomic) IBOutlet UITextField *titleTF;
@property (weak, nonatomic) IBOutlet UITextField *scriptTF;
@end

@implementation NewScriptVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onCancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^(){
        
    }];
}

- (IBAction)onTitleDone:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)onScriptDone:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)onSave:(id)sender {

    
    NSString *title = self.titleTF.text;
    NSString *script = self.scriptTF.text;
    
    if([title isEqualToString:@""] || [script isEqualToString:@""]) {
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完成" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return ;
    }
    NSLog(@"title :%@",title);
    NSLog(@"script :%@",script);
    
    BOOL ret = [[DBHelper sharedDBHelper] insertScript:self.book title:title script:script];
    if (YES == ret) {
        LOG_I("Save script %s Success",[script cStringUsingEncoding:NSUTF8StringEncoding] );
        
    } else {
        LOG_E("Save script %s Failed",[script cStringUsingEncoding:NSUTF8StringEncoding] );
    }
    
    
    [self dismissViewControllerAnimated:YES completion:^(){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"save_script" object:self];
    }];
}

@end
